import { getHomeGoodPriceData, getHomeHighScoreData } from "@/services";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

export const getGoodPriceInfoActionAsync = createAsyncThunk('getGoodPriceInfoAction', (payload, store) => {
  getHomeGoodPriceData().then(res => {
    store.dispatch(updateGoodPriceInfoAction(res))
  })
})
export const getHighScoreActionAsync = createAsyncThunk('getHighScoreAction', async (payload, store) => {
  return await getHomeHighScoreData()
})

const homeSlice = createSlice({
  name: 'home',
  initialState: {
    goodPriceInfo: {},
    highscore: {}
  },
  reducers: {
    updateGoodPriceInfoAction (state, { payload }) {
      state.goodPriceInfo = payload
    }
  },
  extraReducers: (builder) => {
    builder.addCase(getHighScoreActionAsync.fulfilled, (state, { payload }) => {
      state.highscore = payload
    })
  }
})

export const { updateGoodPriceInfoAction } = homeSlice.actions
export default homeSlice.reducer